package com.zz.utils;

import com.zz.dto.AreaOccupancyRate;
import com.zz.dto.OccupancyRate;
import com.zz.pojo.Ward;

import java.util.ArrayList;
import java.util.List;

/**
 * Copyright Zz
 * FileName: WardUtil
 * Author:   Zz
 * Date:     2023/6/1114:17
 * Description:入住信息工具类
 * hISTORY:
 * <author>  <time>     <version>    <desc>
 * 作者姓名   修改时间       版本号       描述
 */
public class WardUtil {
    /**
     * 根据入住信息list计算入住率
     *
     * @param wardList 入住信息list
     * @return 总体入住率以及各个区域的入住率
     */
    public static OccupancyRate calculateOccupancyRate(List<Ward> wardList) {

        List<AreaOccupancyRate> areaOccupancyRates = new ArrayList<>();

        int totalCapacity = 0;// 总容量
        int totalOccupancy = 0;// 总体入住量

        String areaCode = wardList.get(0).getAreaCode();// 区域
        int areaCapacity = 0;// 区域总容量
        int areaOccupancy = 0;// 区域入住量
        float areaOccupancyRate;// 区域入住率

        for (Ward ward : wardList) {
            if (ward.getAreaCode().equals(areaCode)) {
                areaCapacity += 3;
                areaOccupancy += (3 - ward.getNumbers());
            } else {
                totalOccupancy += areaOccupancy;
                totalCapacity += areaCapacity;
                areaOccupancyRate = (float) areaOccupancy / areaCapacity;
                areaOccupancyRates.add(new AreaOccupancyRate(areaCode, areaCapacity, areaOccupancy, areaOccupancyRate));
                areaCode = ward.getAreaCode();
                areaCapacity = 0;
                areaOccupancy = 0;
                areaCapacity += 3;
                areaOccupancy += (3 - ward.getNumbers());
            }
        }


        totalOccupancy += areaOccupancy;
        totalCapacity += areaCapacity;
        areaOccupancyRate = (float) areaOccupancy / areaCapacity;
        areaOccupancyRates.add(new AreaOccupancyRate(areaCode, areaCapacity, areaOccupancy, areaOccupancyRate));

        float totalOccupancyRate = (float) totalOccupancy / totalCapacity;// 总体入住率


        return new OccupancyRate(areaOccupancyRates, totalCapacity, totalOccupancy, totalOccupancyRate);
    }
}
